package office.service.hr.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import office.action.hr.form.CarriageEntryForm;
import office.action.hr.wrapper.CarriageEntryWrapper;
import office.bo.hr.ICarriageEntry;
import office.bo.hr.impl.CarriageEntry;
import office.bo.hr.impl.CarriageManage;
import office.domainset.BizDomainProviderSetImpl;
import office.service.hr.ICarriageEntryService;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class CarriageEntryServiceImpl extends BaseBizServiceImpl implements ICarriageEntryService{

	/**
	 * 获取申请车辆信息
	 * @return
	 * @throws BusinessException
	 */
	public List getCarriageEntryList(String userId) throws BusinessException{
		List listData = new ArrayList() ;
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Map map = new HashMap();
			map.put("userId", userId);
			listData = domainProviderSetImpl.getDao().findByHqlNameAndDomian(domainProviderSetImpl.getCarriageEntryDP(), "findByUserId", map);
			Map mapCarriage = this.getCarriageMap();
			for (int i = 0; i < listData.size(); i++) {
				CarriageEntryWrapper wrapper = (CarriageEntryWrapper)listData.get(i);
				if(null!=wrapper.getCarriageId() && !"".equals(wrapper.getCarriageId())){
					wrapper.setCarriageCode((String)mapCarriage.get(wrapper.getCarriageId()));
				}else{
					wrapper.setCarriageCode("暂未指定");
				}
			}
//			List list = domainProviderSetImpl.getDao().findAllByDomian(domainProviderSetImpl.getCarriageEntryDP());
//			CarriageEntryWrapper wrapper;
//			for(int i=0;i<list.size();i++){
//				CarriageEntry bo = (CarriageEntry)list.get(i);
//				wrapper = new CarriageEntryWrapper();
//				wrapper.copyToWrapper(bo);
//				listData.add(wrapper);
//			}
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取申请车辆信息失败!", e);
		}
		return listData;
	}
	
	public Map getCarriageMap()throws BusinessException{
		Map mapCarriage = new HashMap();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			
			List listData = domainProviderSetImpl.getDao().findAllByDomian(domainProviderSetImpl.getCarriageManageDP());
			for (int i = 0; i < listData.size(); i++) {
				CarriageManage carriageManage = (CarriageManage)listData.get(i);
				mapCarriage.put(carriageManage.getId(), carriageManage.getCarriageCode());
			}
		} catch (Exception e) {
			log.error("", e);
		}
		return mapCarriage;
	}
	/**
	 * 编辑申请车辆
	 * @throws BusinessException
	 */
	public void editCarriageEntry(CarriageEntryForm objctForm)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			CarriageEntry bo = null;
			if(null==objctForm.getId() || ("").equals(objctForm.getId())){
				bo = (CarriageEntry)domainProviderSetImpl.getCarriageEntryDP().newEmptyObj();
			}else{
				bo = (CarriageEntry)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getCarriageEntryDP(), objctForm.getId());
			}
			bo =(CarriageEntry) objctForm.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
				bo.setState("0");
				//把车辆的状态设置为审批中
				//1-23修改(审批的时候才指定使用的车辆)
//				if(null!=objctForm.getCarriageId() && !("").equals(objctForm.getCarriageId())){
//					String carriageId = objctForm.getCarriageId();
//					CarriageManage carriage = (CarriageManage)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getCarriageManageDP(), carriageId);
//					carriage.setState("4");//使用情况=》审批中
//					carriage.store(domainProviderSet);
//				}else{
//					throw new BusinessException("新建申请车辆异常!");
//				}
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑申请车辆信息失败!", e);
		}
	}
	/**
	 * 根据id获取对象
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	public CarriageEntryForm getById(String id)throws BusinessException{
		CarriageEntryForm form = new CarriageEntryForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			ICarriageEntry bo = (ICarriageEntry)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getCarriageEntryDP(), id);
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取申请车辆信息失败!", e);
		}
		return form;
	}
	/**
	 * 根据id删除对象
	 * @param id
	 * @throws BusinessException
	 */
	public void deleteById(String id)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			CarriageEntry bo = (CarriageEntry)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getCarriageEntryDP(), id);
			
			//把车辆的状态设置为可用
			if(null!=bo.getCarriageId() && !("").equals(bo.getCarriageId())){
				String carriageId = bo.getCarriageId();
				CarriageManage carriage = (CarriageManage)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getCarriageManageDP(), carriageId);
				carriage.setState("1");//使用情况=》可用
				carriage.store(domainProviderSet);
			}else{
				new BusinessException("根据id删除申请车辆信息失败!");
			}
			domainProviderSetImpl.getDao().delete(bo);//.deleteById(domainProviderSetImpl.getCarriageEntryDP(), id);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id删除申请车辆信息失败!", e);
		}
	}
	
	/**
	 * 审批车辆申请
	 * @param id
	 * @throws BusinessException
	 */
	public void checkById(String id,String carriageId)throws BusinessException{
		try {
			if(null==carriageId || ("").equals(carriageId)){
				throw new BusinessException("请选择使用车辆!");
			}
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			CarriageEntry bo = (CarriageEntry)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getCarriageEntryDP(), id);
			CarriageManage carriage = (CarriageManage)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getCarriageManageDP(), carriageId);
			String state = Util.parsePropertiesReturnStr("office", "carriageSate.0").split(",")[0];//可用状态
			//查询词车辆使用时间是否已被占用
			Map map = new HashMap();
			map.put("useStartDate", bo.getUseStartDate());
			map.put("useEndDate", bo.getUseEndDate());
			map.put("carriageId", carriageId);
			
			List listData = domainProviderSetImpl.getDao().findByHqlNameAndDomian(domainProviderSetImpl.getCarriageEntryDP(), "findByDate", map);
			if(!carriage.getState().equals(state)){
				throw new BusinessException("请重新选择车辆,此车辆已被使用!");
			}else if(listData.size()>0){
				throw new BusinessException("此申请时间已被其他人申请使用!");
			}
			bo.setState("1");
			bo.setCarriageId(carriageId);
			bo.store(domainProviderSet);
			//2011-2-19 modify 审批成功不需要把车辆设置为使用中
			//carriage.setState("3");//设置车辆使用中
			//carriage.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id审核申请车辆信息失败!", e);
		}
		
	}
}
